<?php

class Software extends CI_Controller {

	public function __construct()  
    {  
        parent::__construct();  
    }

	public function version() {

		$user = $this->input->cookie('user', TRUE);
		if ($user == FALSE) {
			$this->load->helper('url');
			redirect('login/index/please');
		}

		$data['title'] = '软件版本管理';

		$data['page_js'] = '/public/js/page/software/version.js';

		$table = 'version';
		$this->load->model('metro_model');
		$result = $this->metro_model->_rander_filter_options($table);

		foreach ($result as $row) {
			foreach ($row as $key => $value) {
				$value = iconv('GBK', 'UTF-8', $value);
				if(isset($data[$key])){
					if(!in_array($value, $data[$key])){
						$data[$key][] = $value;
					}
				} else {
					$data[$key] = array();
					$data[$key][] = $value;
				}
			}
		}

		$this->load->view('public/header', $data);
		$this->load->view('software/version', $data);
		$this->load->view('public/footer', $data);
	}

	public function get_software_version_data() {
		$request = $this->input->get();
		$query = array();
		if ($request !== FALSE) {
			foreach ($request as $key => $value) {
				$query[$key] = iconv('UTF-8', 'GBK', $value);
			}
		}
		$this->load->model('metro_model');
		$result = $this->metro_model->_get_software_version_data($query);

		$format_result = array(
			'data' => array()
		);
		foreach ($result as $row) {
			$col = array();
			foreach ($row as $key => $value) {
				if ($key == 'downloadtime1' || $key == 'downloadtime2') {
					$col[] = date('Y-m-d', strtotime($value));
				} else {
					$col[] = iconv('GBK', 'UTF-8', $value);
				}
			}
			$format_result['data'][] = $col;
		}
        $this->output->set_content_type('application/json')->set_output(json_encode($format_result));
	}

	public function get_software_version_item() {
		$request = $this->input->get();
		if (!isset($request['id'])) {
			return FALSE;
		}
		$query = array(
			'ID' => (int)$request['id']
		);
		$this->load->model('metro_model');
		$result = $this->metro_model->_get_software_version_item($query);
		$format_result = array();
		if (count($result) > 0) {
			foreach ($result[0] as $key => $value) {
				if ($key == 'downloadtime1' || $key == 'downloadtime2') {
					$format_result[$key] = date('Y-m-d', strtotime($value));
				} else {
					$format_result[$key] = iconv('GBK', 'UTF-8', $value);
				}
			}
			
		}
        $this->output->set_content_type('application/json')->set_output(json_encode($format_result));
	}

	public function add_software_version_data() {
		$request = $this->input->post();
		$query = array();
		if ($request !== FALSE) {
			foreach ($request as $key => $value) {
				$query[$key] = iconv('UTF-8', 'GBK', $value);
			}
		}
		$this->load->model('metro_model');
		$status = $this->metro_model->_add_software_version_data($query);
		$result = array(
			'status' => 0
		);
		if ($status == 1) {
			$result['status'] = 1;
		}
        $this->output->set_content_type('application/json')->set_output(json_encode($result));
	}

	public function update_software_version_data() {
		$request = $this->input->post();
		$query = array();
		foreach ($request as $key => $value) {
			$query[$key] = iconv('UTF-8', 'GBK', $value);
		}
		$this->load->model('metro_model');
		$status = $this->metro_model->_update_software_version_data($query);
		$result = array(
			'status' => 0
		);
		if ($status == 1) {
			$result['status'] = 1;
		}
        $this->output->set_content_type('application/json')->set_output(json_encode($result));
	}

	public function delete_software_version_item() {
		$request = $this->input->post();
		$query = array(
			'ID' => (int)$request['id']
		);
		$this->load->model('metro_model');
		$status = $this->metro_model->_delete_software_version_item($query);
		$result = array(
			'status' => 0
		);
		if ($status == 1) {
			$result['status'] = 1;
		}
        $this->output->set_content_type('application/json')->set_output(json_encode($result));
	}

	public function download() {

		$user = $this->input->cookie('user', TRUE);
		if ($user == FALSE) {
			$this->load->helper('url');
			redirect('login/index/please');
		}

		$data['title'] = '软件下载明细';

		$data['page_js'] = '/public/js/page/software/download.js';

		$table = 'download';
		$this->load->model('metro_model');
		$result = $this->metro_model->_rander_filter_options($table);

		foreach ($result as $row) {
			foreach ($row as $key => $value) {
				$value = iconv('GBK', 'UTF-8', $value);
				if(isset($data[$key])){
					if(!in_array($value, $data[$key])){
						$data[$key][] = $value;
					}
				} else {
					$data[$key] = array();
					$data[$key][] = $value;
				}
			}
		}

		$this->load->view('public/header', $data);
		$this->load->view('software/download', $data);
		$this->load->view('public/footer', $data);
	}

	public function get_software_download_data() {
		$request = $this->input->get();
		$query = array();
		if ($request !== FALSE) {
			foreach ($request as $key => $value) {
				$query[$key] = iconv('UTF-8', 'GBK', $value);
			}
		}
		$this->load->model('metro_model');
		$result = $this->metro_model->_get_software_download_data($query);
		$format_result = array(
			'data' => array()
		);
		foreach ($result as $row) {
			$col = array();
			foreach ($row as $key => $value) {
				if ($key == 'downloadtime' || $key == 'noticetime') {
					$col[] = date('Y-m-d', strtotime($value));
				} else {
					$col[] = iconv('GBK', 'UTF-8', $value);
				}
			}
			$format_result['data'][] = $col;
		}
        $this->output->set_content_type('application/json')->set_output(json_encode($format_result));
	}

	public function get_software_download_item() {
		$request = $this->input->get();
		if (!isset($request['id'])) {
			return FALSE;
		}
		$query = array(
			'ID' => (int)$request['id']
		);
		$this->load->model('metro_model');
		$result = $this->metro_model->_get_software_download_item($query);
		$format_result = array();
		if (count($result) > 0) {
			foreach ($result[0] as $key => $value) {
				if ($key == 'downloadtime' || $key == 'noticetime') {
					$format_result[$key] = date('Y-m-d', strtotime($value));
				} else {
					$format_result[$key] = iconv('GBK', 'UTF-8', $value);
				}
			}
			
		}
        $this->output->set_content_type('application/json')->set_output(json_encode($format_result));
	}

	public function add_software_download_data() {
		$request = $this->input->post();
		$query = array();
		if ($request !== FALSE) {
			foreach ($request as $key => $value) {
				$query[$key] = iconv('UTF-8', 'GBK', $value);
			}
		}
		$this->load->model('metro_model');
		$status = $this->metro_model->_add_software_download_data($query);
		$result = array(
			'status' => 0
		);
		if ($status == 1) {
			$result['status'] = 1;
		}
        $this->output->set_content_type('application/json')->set_output(json_encode($result));
	}

	public function update_software_download_data() {
		$request = $this->input->post();
		$query = array();
		foreach ($request as $key => $value) {
			$query[$key] = iconv('UTF-8', 'GBK', $value);
		}
		$this->load->model('metro_model');
		$status = $this->metro_model->_update_software_download_data($query);
		$result = array(
			'status' => 0
		);
		if ($status == 1) {
			$result['status'] = 1;
		}
        $this->output->set_content_type('application/json')->set_output(json_encode($result));
	}

	public function delete_software_download_item() {
		$request = $this->input->post();
		$query = array(
			'ID' => (int)$request['id']
		);
		$this->load->model('metro_model');
		$status = $this->metro_model->_delete_software_download_item($query);
		$result = array(
			'status' => 0
		);
		if ($status == 1) {
			$result['status'] = 1;
		}
        $this->output->set_content_type('application/json')->set_output(json_encode($result));
	}

	public function get_options() {
		$this->load->model('metro_model');
		$result = $this->metro_model->_get_options();

		$format_result = array();
		foreach ($result as $row) {
			if (isset($format_result[$row['attr']])) {
				if (in_array($row['info'], $format_result[$row['attr']])) {
					break;
				}
			} else {
				$format_result[$row['attr']] = array();
			}
			$format_result[$row['attr']][] = iconv('GBK', 'UTF-8', $row['info']);
		}
        $this->output->set_content_type('application/json')->set_output(json_encode($format_result));
	}

	public function sort() {

		$user = $this->input->cookie('user', TRUE);
		if ($user == FALSE) {
			$this->load->helper('url');
			redirect('login/index/please');
		}

		$data['title'] = '分类查询';

		$data['page_js'] = '/public/js/page/software/sort.js';

		$data['sort'] = $this->get_sort_options();

		$this->load->view('public/header', $data);
		$this->load->view('software/sort', $data);
		$this->load->view('public/footer', $data);
	}

	public function get_finished_data() {
		$request = $this->input->get();
		$query = array();
		if ($request !== FALSE) {
			foreach ($request as $key => $value) {
				$query[$key] = iconv('UTF-8', 'GBK', $value);
			}
		}
		$this->load->model('metro_model');
		$result = $this->metro_model->_get_download_finished_data($query);

		$format_result = array(
			'data' => array()
		);
		foreach ($result as $row) {
			$col = array();
			foreach ($row as $key => $value) {
				if ($key == 'downloadtime2') {
					$col[] = date('Y-m-d', strtotime($value));
				} else {
					$col[] = iconv('GBK', 'UTF-8', $value);
				}
			}
			$format_result['data'][] = $col;
		}
        $this->output->set_content_type('application/json')->set_output(json_encode($format_result));
	}

	public function get_unfinished_data() {
		$request = $this->input->get();
		$query = array();
		if ($request !== FALSE) {
			foreach ($request as $key => $value) {
				$query[$key] = iconv('UTF-8', 'GBK', $value);
			}
		}
		$this->load->model('metro_model');
		$result = $this->metro_model->_get_download_unfinished_data($query);

		$format_result = array(
			'data' => array()
		);
		foreach ($result as $row) {
			$col = array();
			foreach ($row as $key => $value) {
				if ($key == 'downloadtime') {
					$col[] = date('Y-m-d', strtotime($value));
				} else {
					$col[] = iconv('GBK', 'UTF-8', $value);
				}
			}
			$format_result['data'][] = $col;
		}
        $this->output->set_content_type('application/json')->set_output(json_encode($format_result));
	}

	public function get_back_data() {
		$request = $this->input->get();
		$query = array();
		if ($request !== FALSE) {
			foreach ($request as $key => $value) {
				if ($key == 'finish' || $key == 'back') {
					$query[$key] = intval($value);
				} else {
					$query[$key] = iconv('UTF-8', 'GBK', $value);
				}
			}
		}
		$this->load->model('metro_model');
		$result = $this->metro_model->_get_back_data($query);

		$format_result = array(
			'data' => array()
		);
		foreach ($result as $row) {
			$col = array();
			foreach ($row as $key => $value) {
				$col[] = iconv('GBK', 'UTF-8', $value);
			}
			$format_result['data'][] = $col;
		}
        $this->output->set_content_type('application/json')->set_output(json_encode($format_result));
	}

	public function get_downloadtime_data() {
		$request = $this->input->get();
		$query = array();
		if ($request !== FALSE) {
			foreach ($request as $key => $value) {
				$query[$key] = iconv('UTF-8', 'GBK', $value);
			}
		}
		$this->load->model('metro_model');
		$result = $this->metro_model->_get_version_download_time($query);
		$format_result = array(
			'data' => array()
		);
		foreach ($result as $row) {
			$col = array();
			foreach ($row as $key => $value) {
				$col[] = iconv('GBK', 'UTF-8', $value);
			}
			$format_result['data'][] = $col;
		}
        $this->output->set_content_type('application/json')->set_output(json_encode($format_result));
	}

	public function get_back_detail() {
		$request = $this->input->get();
		$query = array();
		if ($request !== FALSE) {
			foreach ($request as $key => $value) {
				if ($key == 'finish' || $key == 'back') {
					$query[$key] = intval($value);
				} else {
					$query[$key] = iconv('UTF-8', 'GBK', $value);
				}
			}
		}
		$this->load->model('metro_model');
		$result = $this->metro_model->_get_back_detail($query);
		foreach ($result as $row) {
			$col = array();
			foreach ($row as $key => $value) {
				if ($key == 'downloadtime1' || $key == 'downloadtime2') {
					$col[] = date('Y-m-d', strtotime($value));
				} else {
					$col[] = iconv('GBK', 'UTF-8', $value);
				}
			}
			$format_result['data'][] = $col;
		}
        $this->output->set_content_type('application/json')->set_output(json_encode($format_result));
	}

	public function get_sort_options() {
		$query = array();
		$filters = array(
			'download_finished' => array('area', 'device', 'type'),
			'download_unfinished' => array('area', 'device', 'type', 'line'),
			'back' => array('supplier', 'device', 'type'),
			'downloadtime' => array('center', 'device', 'type')
		);
		$result = array();
		$this->load->model('metro_model');
		$result['download_finished'] = $this->metro_model->_get_download_finished_data($query);
		$result['download_unfinished'] = $this->metro_model->_get_download_unfinished_data($query);
		$result['back'] = $this->metro_model->_get_back_data($query);
		$result['downloadtime'] = $this->metro_model->_get_version_download_time($query);

		$format_result = array();
		foreach ($result as $sort => $table) {
			$format_result[$sort] = array();
			foreach ($table as $row) {
				foreach ($row as $key => $value) {
					$value = iconv('GBK', 'UTF-8', $value);
					if (in_array($key, $filters[$sort])) {
						if(isset($format_result[$sort][$key])){
							if(!in_array($value, $format_result[$sort][$key])){
								$format_result[$sort][$key][] = $value;
							}
						} else {
							$format_result[$sort][$key] = array();
							$format_result[$sort][$key][] = $value;
						}
					}
				}
			}
		}

		return $format_result;
	}
}

?>